package com.easywan.web.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.easywan.core.JSPModelAndView;
import com.easywan.core.RedirectModelAndView;
import com.easywan.service.ArticleService;
import com.easywan.service.EasywanSystemIndexService;
import com.easywan.service.IWanActiveService;
import com.easywan.service.SpotsService;
import com.easywan.util.Utils;
import com.easywan.vo.Article;
import com.easywan.vo.EasywanSystemIndex;
import com.easywan.vo.Pages;
import com.easywan.vo.Spots;
import com.easywan.vo.WanActive;

@Controller
@RequestMapping("admin/EasywanSystemIndex")
public class EasywanSystemIndexControllerAdmin {
	private static Logger logger = Logger.getLogger(EasywanSystemIndexControllerAdmin.class);
	@Autowired 
	EasywanSystemIndexService service;
	@Autowired
	ArticleService articleService;
	@Autowired
	SpotsService spotsService;
	@Autowired
	IWanActiveService wanActiveService;
	@RequestMapping("index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response,Pages pages,
			@RequestParam("pname")String pname,
			@RequestParam("name")String name){
		JSPModelAndView view = new JSPModelAndView("/admin/EasywanSystemIndex/index");
		EasywanSystemIndex tempEasywanSystemIndex = new EasywanSystemIndex();
		tempEasywanSystemIndex.setPage(pages);
		List<EasywanSystemIndex> lists = service.list(tempEasywanSystemIndex);
		view.addObject("data",lists);
		view.addObject("pname", pname);
		view.addObject("name", name);
		view.addObject("page", pages);
		return view;
	}
	@RequestMapping("add")
	public ModelAndView add(HttpServletRequest request,HttpServletResponse response,
			@RequestParam("pname")String pname,@RequestParam("name")String name){
		JSPModelAndView view = new JSPModelAndView("/admin/EasywanSystemIndex/modify");
		view.addObject("pname", pname);
		view.addObject("name", name);
		view.addObject("action", "add");
		return view;
	}
	@RequestMapping("add_chk")
	public ModelAndView add_chk(HttpServletRequest request,HttpServletResponse response,EasywanSystemIndex EasywanSystemIndex){
		ModelAndView view = new RedirectModelAndView("admin/EasywanSystemIndex/index");
		service.add(EasywanSystemIndex);
		view.addObject("pname","");
		view.addObject("name","");
		return view;
	}
	
	@RequestMapping("modify")
	public ModelAndView modify(HttpServletRequest request,HttpServletResponse response,
			@RequestParam("pname")String pname,@RequestParam("name")String name,
			@RequestParam("id")Integer id){
		EasywanSystemIndex EasywanSystemIndex = service.findById(id);
		JSPModelAndView view = null;
		
		switch(EasywanSystemIndex.getName()){
		default:
		case "slider":
			view = new JSPModelAndView("/admin/EasywanSystemIndex/modify");
			break;
		case "tj1":
		case "tj2":
			view = new JSPModelAndView("/admin/EasywanSystemIndex/modify");
			break;
		case "usergl":
		case "xiaobiantuijian":
		case "xiaobiantuijianph":
		case "gonglueph":
		case "xitongtuijph":
		case "userglph":
		case "gonglue":
		case "xitongtuij":
			view = new JSPModelAndView("/admin/EasywanSystemIndex/gonglue");
			break;
		}
			
		
		view.addObject("pname", pname);
		view.addObject("name", name);
		view.addObject("action", "modify");
		view.addObject("data",EasywanSystemIndex);
		return view;
	}
	@RequestMapping("modify_chk")
	public ModelAndView modify_chk(HttpServletRequest request,HttpServletResponse response,EasywanSystemIndex EasywanSystemIndex){
		ModelAndView view = new RedirectModelAndView("admin/EasywanSystemIndex/index");
		switch(EasywanSystemIndex.getName()){
		default:
		case "slider":
			break;
		case "tj1":
		case "tj2":
			break;
		case "xiaobiantuijian":
			WanActive tmpWanActive = wanActiveService.findById(Integer.valueOf(EasywanSystemIndex.getValue1()));
			System.out.println(tmpWanActive);
			EasywanSystemIndex.setValue2(tmpWanActive.getTitle());
			EasywanSystemIndex.setValue3(tmpWanActive.getCreatetime().toString());
			break;
		case "usergl":
			Spots tmpSpots = spotsService.findById(Integer.valueOf(EasywanSystemIndex.getValue1()));
			System.out.println(tmpSpots);
			EasywanSystemIndex.setValue2(tmpSpots.getTitle());
			EasywanSystemIndex.setValue3(tmpSpots.getCreatetime().toString());
			break;
		
		case "xitongtuij":
		
		case "xiaobiantuijianph":
		case "gonglue":
		case "gonglueph":
		case "xitongtuijph":
		case "userglph":
			Article tmp = articleService.findById(Integer.valueOf(EasywanSystemIndex.getValue1()));
			EasywanSystemIndex.setValue2(tmp.getTitle());
			EasywanSystemIndex.setValue3(tmp.getCreatetime().toString());
			break;
		
		}
		service.modify(EasywanSystemIndex);
		view.addObject("pname","");
		view.addObject("name","");
		return view;
	}
	@RequestMapping("del")
	public void del(HttpServletRequest request,HttpServletResponse response,@RequestParam("id")Integer id){
		service.deleteById(id);
		Utils.sendSuccessMsg(response);
	}
}
